package com.example.demo.dynamic;

import javassist.*;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.ParameterAnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.ArrayMemberValue;
import javassist.bytecode.annotation.MemberValue;
import javassist.bytecode.annotation.StringMemberValue;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class main {

    private static String savePath = "G:\\博士学习\\工作\\数据库\\实现\\new\\result\\result.txt";
    private static String classes = "G:\\博士学习\\工作\\数据库\\实现\\new\\target\\classes";


    public static void insertCodeToDealLogin() throws Exception{
        ClassPool pool = ClassPool.getDefault();
        pool.insertClassPath(classes);//executeSql类所在位置
        CtClass cc = pool.get("com.example.demo.dealLogin");
        CtMethod Login = cc.getDeclaredMethod("Login");
        CtMethod test = cc.getDeclaredMethod("testJson");
        pool.importPackage("com.example.demo");
        pool.importPackage("javax.servlet.http.HttpServletRequest");

        try{
            //插入新的获取ip的方法
            String methodgetIp = "public static String getIp(HttpServletRequest request) {\n" +
                    "        String LOCAL_IP = \"127.0.0.1\";\n" +
                    "        if (request == null) {\n" +
                    "            return \"unknown\";\n" +
                    "        }\n" +
                    "        String ip = request.getHeader(\"x-forwarded-for\");\n" +
                    "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                    "            ip = request.getHeader(\"Proxy-Client-IP\");\n" +
                    "        }\n" +
                    "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                    "            ip = request.getHeader(\"X-Forwarded-For\");\n" +
                    "        }\n" +
                    "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                    "            ip = request.getHeader(\"WL-Proxy-Client-IP\");\n" +
                    "        }\n" +
                    "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                    "            ip = request.getHeader(\"X-Real-IP\");\n" +
                    "        }\n" +
                    "\n" +
                    "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                    "            ip = request.getRemoteAddr();\n" +
                    "        }\n" +
                    "\n" +
                    "        return \"0:0:0:0:0:0:0:1\".equals(ip) ? LOCAL_IP : ip;\n" +
                    "    }";
            CtMethod new_getIp = CtNewMethod.make(methodgetIp, cc);
            cc.addMethod(new_getIp);
        }catch (Exception e){
            System.out.println(e);
        }

        Login.setBody("{\n" +
                "        if ($1.getSession().getAttribute(\"username\") != null){\n" +
                "            System.out.println($1.getSession().getAttribute(\"username\"));\n" +
                "            System.out.println($1.getSession().getAttribute(\"password\"));\n" +
                "        }\n" +
                "        String username=$1.getParameter(\"username\");\n" +
                "        String password=$1.getParameter(\"password\");\n" +
                "        $1.getSession().setAttribute(\"username\",username);\n" +
                "        $1.getSession().setAttribute(\"password\",password);\n" +
                "        System.out.println(username);\n" +
                "        System.out.println(password);\n" +
                "\n" +
                "        executeSql es = new executeSql();\n" +
                "        String id = es.selectUserAndPassword(getIp($1),username,password);\n" +
                "        if(id != null){\n" +
                "            $1.getSession().setAttribute(\"user_id\",id);\n" +
                "            return \"index\";\n" +
                "        }\n" +
                "\n" +
                "        return \"main\";\n" +
                "    }");

        test.setBody("{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "executeSql es = new executeSql();\n" +
                "        String userid = es.selectUserAndPassword(ip, $1.getUsername(),$1.getPassword());\n" +
                "        if(userid  == null)\n" +
                "            $1.setUserid(0);\n" +
                "        else\n" +
                "            $1.setUserid(Integer.parseInt(userid));\n" +
                "        return $1;}");



        /*


        String methodLogin = "public String Login(HttpServletRequest request){\n" +
                "        if (request.getSession().getAttribute(\"username\") != null){\n" +
                "            System.out.println(request.getSession().getAttribute(\"username\"));\n" +
                "            System.out.println(request.getSession().getAttribute(\"password\"));\n" +
                "        }\n" +
                "        String username=request.getParameter(\"username\");\n" +
                "        String password=request.getParameter(\"password\");\n" +
                "        request.getSession().setAttribute(\"username\",username);\n" +
                "        request.getSession().setAttribute(\"password\",password);\n" +
                "        System.out.println(username);\n" +
                "        System.out.println(password);\n" +
                "\n" +
                "        executeSql es = new executeSql();\n" +
                "        String id = es.selectUserAndPassword(getIp(request),username,password);\n" +
                "        if(id != null){\n" +
                "            request.getSession().setAttribute(\"user_id\",id);\n" +
                "            return \"index\";\n" +
                "        }\n" +
                "\n" +
                "        return \"main\";\n" +
                "    }";
        CtMethod new_Login= CtNewMethod.make(methodLogin, cc);


        ClassFile ccFile = cc.getClassFile();
        ConstPool constpool = ccFile.getConstPool();


        new_Login.getMethodInfo().addAttribute(addAnnotationMethod(cc,"/login"));
        cc.removeMethod(Login);//删除旧方法
        cc.addMethod(new_Login);*/
        System.out.println("修改完成!!!");
        cc.writeFile(classes);

    }


    //向myController类中插入代码
    public static  void  insertCodeToMyController() throws  Exception{

        ClassPool pool = ClassPool.getDefault();
        pool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader()));
        pool.insertClassPath(classes);//executeSql类所在位置
        pool.insertClassPath("D:\\Softwares\\Programming\\apache-maven-3.8.3\\repository\\");
        CtClass cc = pool.get("com.example.demo.myController");
        CtMethod getDetails = cc.getDeclaredMethod("getDetails");

        CtMethod getGoods = cc.getDeclaredMethod("getGoods");
        CtMethod getOrders = cc.getDeclaredMethod("getOrders");

        CtMethod updateDetails = cc.getDeclaredMethod("updateDetails");
        CtMethod insertOrder = cc.getDeclaredMethod("insertOrder");

        //导包
        pool.importPackage("com.example.demo");
        pool.importPackage("com.example.demo.data");
        pool.importPackage("javax.servlet.http.HttpServletRequest");
        pool.importPackage("org.springframework.web.context.request");
        pool.importPackage("org.springframework.web.context.request.RequestContextHolder");
        pool.importPackage("org.springframework.web.context.request.ServletRequestAttributes");


        //插入新的获取ip的方法
        String methodgetIp = "public static String getIp(HttpServletRequest request) {\n" +
                "        String LOCAL_IP = \"127.0.0.1\";\n" +
                "        if (request == null) {\n" +
                "            return \"unknown\";\n" +
                "        }\n" +
                "        String ip = request.getHeader(\"x-forwarded-for\");\n" +
                "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                "            ip = request.getHeader(\"Proxy-Client-IP\");\n" +
                "        }\n" +
                "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                "            ip = request.getHeader(\"X-Forwarded-For\");\n" +
                "        }\n" +
                "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                "            ip = request.getHeader(\"WL-Proxy-Client-IP\");\n" +
                "        }\n" +
                "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                "            ip = request.getHeader(\"X-Real-IP\");\n" +
                "        }\n" +
                "\n" +
                "        if (ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\n" +
                "            ip = request.getRemoteAddr();\n" +
                "        }\n" +
                "\n" +
                "        return \"0:0:0:0:0:0:0:1\".equals(ip) ? LOCAL_IP : ip;\n" +
                "    }";
        CtMethod new_getIp = CtNewMethod.make(methodgetIp, cc);
        cc.addMethod(new_getIp);

        //-----修改getDetails-----
        String methodgetDetails = "{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "\n" +
                "        executeSql es = new executeSql();\n" +
                "        $1 = es.selectDetail($1.getUserId(), ip);\n" +
                "\n" +
                "        return $1;}";
        getDetails.setBody(methodgetDetails);





        //-----修改getGoods-----
        String methodgetGoods = "{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "        executeSql es = new executeSql();\n" +
                "        $1 = es.selectGoods(ip);\n" +
                "\n" +
                "        return $1;}";
        getGoods.setBody(methodgetGoods);


        //-----修改getOrders-----
        String methodgetOrders = "{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "        executeSql es = new executeSql();\n" +
                "        $1 = es.selectOrders(ip);\n" +
                "\n" +
                "        return $1;}";
        getOrders.setBody(methodgetOrders);


        //-----修改updateDetails-----
        String methodupdateDetails = "{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "        executeSql es = new executeSql();\n" +
                "        es.updateDetail($1, ip);\n" +
                "\n" +
                "\n" +
                "        return $1;}";
        updateDetails.setBody(methodupdateDetails);


        //-----修改insertOrder-----
        String methodinsertOrder = "{HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();\n" +
                "        String ip = getIp(request);\n" +
                "        executeSql es = new executeSql();\n" +
                "        es.insertOrder($1, ip);\n" +
                "\n" +
                "\n" +
                "        return $1;}";
        insertOrder.setBody(methodinsertOrder);


        System.out.println("修改完成!!!");
        cc.writeFile(classes);
    }

    public static AnnotationsAttribute addAnnotationMethod(CtClass cc,String value){
        ClassFile ccFile = cc.getClassFile();
        ConstPool constpool = ccFile.getConstPool();
        AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
        Annotation annot1 = new Annotation("RequestMapping", constpool);


        StringMemberValue[] elements = {new StringMemberValue(value,constpool)};
        ArrayMemberValue amv = new ArrayMemberValue(constpool);
        amv.setValue(elements);


        annot1.addMemberValue("value", amv);
        Annotation annot2 = new Annotation("ResponseBody", constpool);
        attr.addAnnotation(annot1);
        attr.addAnnotation(annot2);
        return attr;
    }

    public static ParameterAnnotationsAttribute addParameterAnnotation(CtClass cc){
        ClassFile ccFile = cc.getClassFile();
        ConstPool constpool = ccFile.getConstPool();
        ParameterAnnotationsAttribute parameterAtrribute = new ParameterAnnotationsAttribute(
                constpool, ParameterAnnotationsAttribute.visibleTag);
        Annotation paramAnnot = new Annotation("RequestBody",constpool);
        //paramAnnot.addMemberValue("value", new StringMemberValue("",constPool));
        Annotation[][] paramArrays = new Annotation[1][1];
        paramArrays[0][0] = paramAnnot;
        parameterAtrribute.setAnnotations(paramArrays);
        return parameterAtrribute;
    }

    //向executeSql类中插入代码
    public static  void insertCodeToExecuteSql() throws Exception{
        ClassPool pool = ClassPool.getDefault();
        pool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader()));
        pool.insertClassPath(classes);//executeSql类所在位置
        CtClass cc = pool.get("com.example.demo.executeSql");
        CtMethod selectUserAndPassword = cc.getDeclaredMethod("selectUserAndPassword");

        CtMethod selectDetail = cc.getDeclaredMethod("selectDetail");

        CtMethod updateDetail = cc.getDeclaredMethod("updateDetail");
        CtMethod selectGoods = cc.getDeclaredMethod("selectGoods");
        CtMethod selectOrders = cc.getDeclaredMethod("selectOrders");
        CtMethod insertOrder = cc.getDeclaredMethod("insertOrder");

        //插入新的字段
        try{
            cc.addField(CtField.make("private static String savePath = \"G:/博士学习/工作/数据库/实现/new/result/result.txt\";", cc));
        }catch (Exception e){
            System.out.println(e);
        }


        //添加新的保存ip和sql的方法
        //先导包
        pool.importPackage("java.io.File");
        pool.importPackage("java.io.FileWriter");
        pool.importPackage("java.io.IOException");
        pool.importPackage("java.io.PrintWrite");
        pool.importPackage("java.sql.ResultSet");
        pool.importPackage("com.example.demo");
        pool.importPackage("com.example.demo.data");
        pool.importPackage("java.sql.SQLException");
        pool.importPackage("java.text.SimpleDateFormat");
        pool.importPackage("java.util.Date");


        try{
            String methodSaveInfoStr = "public static void saveInfo(String ip, String sql){\n" +
                    "        FileWriter fw = null;\n" +
                    "        try {\n" +
                    "            File f=new File(savePath);\n" +
                    "            fw = new FileWriter(f, true);\n" +
                    "\n" +
                    "        }catch (Exception e){\n" +
                    "            System.out.println(e);\n" +
                    "        }\n" +
                    "        SimpleDateFormat df = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");//设置日期格式\n" +
                    "        //System.out.println(df.format(new Date()));// new Date()为获取当前系统时间\n" +
                    "        java.io.PrintWriter pw = new java.io.PrintWriter(fw);\n" +
                    "        pw.println(ip+\"#\"+sql+\"#\"+df.format(new Date()));\n" +
                    "        pw.flush();\n" +
                    "        try {\n" +
                    "            fw.flush();\n" +
                    "            pw.close();\n" +
                    "            fw.close();\n" +
                    "        } catch (IOException e) {\n" +
                    "            e.printStackTrace();\n" +
                    "        }\n" +
                    "\n" +
                    "    }";
            CtMethod new_saveInfo = CtNewMethod.make(methodSaveInfoStr, cc);
            cc.addMethod(new_saveInfo);
        }catch (Exception e){
            System.out.println(e);
        }


        //-----修改方法selectUserAndPassword-----
        String methodSelectAndPassword = "public String selectUserAndPassword(String ip, String username, String password){\n" +
                "        ResultSet ret = null;\n" +
                "\n" +
                "\n" +
                "        String sql = \"select * from users where name = \\'\"+username+\"\\' and password = \\'\"+password+\"\\';\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db1 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        String uid = null;\n" +
                "        saveInfo(ip,sql);\n" +
                "        try {\n" +
                "            ret = db1.pst.executeQuery();//执行语句，得到结果集\n" +
                "            while (ret.next()) {\n" +
                "                uid = ret.getString(1);\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "        return uid;\n" +
                "    }";
        cc.removeMethod(selectUserAndPassword);//删除旧方法
        CtMethod new_selectUserAndPassword = CtNewMethod.make(methodSelectAndPassword, cc);//创建新方法
        cc.addMethod(new_selectUserAndPassword);//添加新方法


        //-----修改selectDetails-----
        String methodselectDetail = "public Details selectDetail(int userId, String ip){\n" +
                "\n" +
                "        ResultSet ret = null;\n" +
                "        Details details = new Details();\n" +
                "\n" +
                "\n" +
                "        String sql = \"select * from details where user_id =\"+ userId + \";\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db1 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        saveInfo(ip,sql);\n" +
                "        try {\n" +
                "            ret = db1.pst.executeQuery();//执行语句，得到结果集\n" +
                "            while (ret.next()) {\n" +
                "                details.setUserId(ret.getInt(1));\n" +
                "                details.setUserName(ret.getString(2));\n" +
                "                details.setAddress(ret.getString(3));\n" +
                "                details.setAge(ret.getInt(4));\n" +
                "                int sex = ret.getInt(5);\n" +
                "                if(sex == 0)\n" +
                "                    details.setSex(\"女\");\n" +
                "                else\n" +
                "                    details.setSex(\"男\");\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "        return details;\n" +
                "    }";
        cc.removeMethod(selectDetail);//删除旧方法
        CtMethod new_selectDetail= CtNewMethod.make(methodselectDetail, cc);//创建新方法
        cc.addMethod(new_selectDetail);//添加新方法

        //----修改updateDetail----
        String methodupdateDetail = "public void updateDetail(Details details, String ip){\n" +
                "        int sex = 0;\n" +
                "        if(details.getSex()==\"男\")\n" +
                "            sex = 1;\n" +
                "        String sql = \"update details set address =\\'\"+details.getAddress()+\"\\',age=\"+details.getAge()+\",sex=\"+sex+\" where user_id=\"+details.getUserId()+\";\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db1 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        saveInfo(ip,sql);\n" +
                "        try {\n" +
                "            db1.pst.executeUpdate();//执行语句，得到结果\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "    }";
        cc.removeMethod(updateDetail);//删除旧方法
        CtMethod new_updateDetail = CtNewMethod.make(methodupdateDetail, cc);//创建新方法
        cc.addMethod(new_updateDetail);//添加新方法


        //-----修改selectGoods---
        String methodselectGoods = "public Goods selectGoods(String ip){\n" +
                "        Goods good = new Goods();\n" +
                "        ResultSet ret = null;\n" +
                "\n" +
                "        String sql = \"select * from goods;\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db1 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        saveInfo(ip,sql);\n" +
                "        int[] goodId = null;\n" +
                "        String[] goodName = null;\n" +
                "        double[] goodPrice = null;\n" +
                "\n" +
                "\n" +
                "        try {\n" +
                "            ret = db1.pst.executeQuery();//执行语句，得到结果集\n" +
                "            ret.last();\n" +
                "            int count = ret.getRow();\n" +
                "            ret.beforeFirst();\n" +
                "            System.out.println(\"----------:\"+count);\n" +
                "            goodId = new int[count];\n" +
                "            goodName = new String[count];\n" +
                "            goodPrice = new double[count];\n" +
                "            count = 0;\n" +
                "            while (ret.next()) {\n" +
                "                goodId[count] = ret.getInt(1);\n" +
                "                goodName[count] = ret.getString(2);\n" +
                "                goodPrice[count] = ret.getDouble(3);\n" +
                "                ++count;\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "        good.setGoodId(goodId);\n" +
                "        good.setGoodName(goodName);\n" +
                "        good.setGoodPrice(goodPrice);\n" +
                "\n" +
                "\n" +
                "        return good;\n" +
                "    }";
        cc.removeMethod(selectGoods);//删除旧方法
        CtMethod new_selectGoods = CtNewMethod.make(methodselectGoods, cc);//创建新方法
        cc.addMethod(new_selectGoods);//添加新方法


        //-----修改selectOrders------
        String methodselectOrders = "public Orders selectOrders(String ip){\n" +
                "        Orders order = new Orders();\n" +
                "        ResultSet ret = null;\n" +
                "\n" +
                "        String sql = \"select * from data.show;\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db1 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        saveInfo(ip,sql);\n" +
                "        int[] orderId = null;\n" +
                "        String[] userName = null;\n" +
                "        String[] address = null;\n" +
                "        String[] goodName = null;\n" +
                "\n" +
                "\n" +
                "        try {\n" +
                "            ret = db1.pst.executeQuery();//执行语句，得到结果集\n" +
                "            ret.last();\n" +
                "            int count = ret.getRow();\n" +
                "            ret.beforeFirst();\n" +
                "            System.out.println(\"----------:\"+count);\n" +
                "            orderId = new int[count];\n" +
                "            userName = new String[count];\n" +
                "            address = new String[count];\n" +
                "            goodName = new String[count];\n" +
                "            count = 0;\n" +
                "            while (ret.next()) {\n" +
                "                orderId[count] = ret.getInt(3);\n" +
                "                userName[count] = ret.getString(1);\n" +
                "                address[count] = ret.getString(2);\n" +
                "                goodName[count] = ret.getString(4);\n" +
                "                ++count;\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "        order.setOrderId(orderId);\n" +
                "        order.setUserName(userName);\n" +
                "        order.setAddress(address);\n" +
                "        order.setGoodName(goodName);\n" +
                "\n" +
                "\n" +
                "        return order;\n" +
                "    }";
        cc.removeMethod(selectOrders);//删除旧方法
        CtMethod new_selectOrders = CtNewMethod.make(methodselectOrders, cc);//创建新方法
        cc.addMethod(new_selectOrders);//添加新方法


        //----修改insertOrder-------
        String methodinsertOrder = "public void insertOrder(Orders order, String ip){\n" +
                "        String sql_user_id = \"select user_id from users where name = \\'\"+order.getUserName()[0]+\"\\';\";\n" +
                "\n" +
                "\n" +
                "        ResultSet ret = null;\n" +
                "        System.out.println(sql_user_id);\n" +
                "        connectMysql db1 = new connectMysql(sql_user_id);//创建DBHelper对象\n" +
                "        String user_id = null;\n" +
                "        saveInfo(ip,sql_user_id);\n" +
                "        try {\n" +
                "            ret = db1.pst.executeQuery();//执行语句，得到结果集\n" +
                "            while (ret.next()) {\n" +
                "                user_id = ret.getString(1);\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db1.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "        String sql_good_id = \"select good_id from goods where good_name = \\'\"+order.getGoodName()[0]+\"\\';\";\n" +
                "        System.out.println(sql_good_id);\n" +
                "        connectMysql db2 = new connectMysql(sql_good_id);//创建DBHelper对象\n" +
                "        String good_id = null;\n" +
                "        saveInfo(ip,sql_good_id);\n" +
                "        try {\n" +
                "            ret = db2.pst.executeQuery();//执行语句，得到结果集\n" +
                "            while (ret.next()) {\n" +
                "                good_id = ret.getString(1);\n" +
                "            }//显示数据\n" +
                "            ret.close();\n" +
                "            db2.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "\n" +
                "        String sql = \"insert into orders(user_id,good_id,good_name,time) values(\"+user_id+\",\"+good_id+\",\\'\"+order.getGoodName()[0]+\"\\',\\'20211027\\');\";//SQL语句\n" +
                "        System.out.println(sql);\n" +
                "        connectMysql db3 = new connectMysql(sql);//创建DBHelper对象\n" +
                "        saveInfo(ip,sql);\n" +
                "\n" +
                "\n" +
                "        try {\n" +
                "            db3.pst.executeUpdate();//执行语句，得到结果集\n" +
                "            db3.close();//关闭连接\n" +
                "        } catch (SQLException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }";
        cc.removeMethod(insertOrder);//删除旧方法
        CtMethod new_insertOrder = CtNewMethod.make(methodinsertOrder, cc);//创建新方法
        cc.addMethod(new_insertOrder);//添加新方法




        System.out.println("修改完成!!!");
        cc.writeFile(classes);

    }

    public static void saveInfo(String ip, String sql){
        FileWriter fw = null;
        try {
            File f=new File(savePath);
            fw = new FileWriter(f, true);

        }catch (Exception e){
            System.out.println(e);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        PrintWriter pw = new PrintWriter(fw);
        pw.println(ip+" "+sql+" "+df.format(new Date()));
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static String getIp(HttpServletRequest request) {
        String LOCAL_IP = "127.0.0.1";
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? LOCAL_IP : ip;
    }

    public static void main(String[] args) {
        try {
            insertCodeToExecuteSql();
            insertCodeToMyController();
            insertCodeToDealLogin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
